package com.lagou.homework2;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class StudentMapTest {

    public static void main(String[] args) {

        Map<Student,String> map1 = new HashMap<Student,String>();
        map1.put(new Student("张三", 20), "北京");
        map1.put(new Student("李四", 25), "上海");
        map1.put(new Student("张飞", 30), "广州");
        map1.put(new Student("关羽", 40), "深圳");
        map1.put(new Student("刘备", 45), "南京");
        //获取Map集合中所有的键值对并组成Set视图
        Set<Map.Entry<Student, String>> entries = map1.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            System.out.println("得到key的值为：" + entry.getKey() + ",地址为：" + entry.getValue());
        }
        System.out.println("---------------------------");
        //获取Map集合中所有的key并组成Set视图
        Set<Student> s1 = map1.keySet();
        for (Student student : s1) {
            System.out.println("得到key的值为：" + student + ",地址为：" + map1.get(student));
        }
        System.out.println("---------------------------------------");
        // 通过迭代器(entrySet)
        Iterator<Map.Entry<Student, String>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<Student, String> entry = iterator.next();
            System.out.println("得到key的值为：" + entry.getKey() + ",地址为：" + entry.getValue());
        }
        System.out.println("------------------------------------------------------");
        // 通过迭代器(keySet)
        Iterator<Student> iterator1 = s1.iterator();
        while (iterator1.hasNext()) {
            Student student = iterator1.next();
            System.out.println("得到key的值为：" + student + ",地址为：" + map1.get(student));
        }

    }
}
